package com.acheli.mixin;


import com.acheli.rideable.entites.ComponentEntity;
import com.acheli.rideable.entites.RideableEntity;
import com.acheli.rideable.functionLathes.ComponentFL;
import com.acheli.rideable.functionLathes.component.SeatFL;
import com.acheli.rideable.functionLathes.component.compile.EventToJS;
import com.mojang.blaze3d.platform.InputConstants;
import net.minecraft.client.Minecraft;
import net.minecraft.client.MouseHandler;
import net.minecraft.world.entity.player.Player;
import org.spongepowered.asm.mixin.*;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;

@Mixin(MouseHandler.class)
public class MouseMixin {

    @Final
    @Mutable
    @Shadow
    private Minecraft minecraft;
    @Unique
    private double lastX = 0;
    @Unique
    private double lastY = 0;

    @Inject(method = "onPress", at = @At("HEAD"))
    private void onPress(long pWindowPointer, int pButton, int pAction, int pModifiers, CallbackInfo ci) {
        Player player = Minecraft.getInstance().player;
        InputConstants.Key inputKey = InputConstants.Type.MOUSE.getOrCreate(pButton);
        if (player != null && player.getVehicle() instanceof ComponentEntity componentEntity) {
            componentEntity.doFactory((factoryNotNull)->{
                factoryNotNull.getProductFunction().runScript(new EventToJS("onPress", inputKey, pAction));
            });
            if (componentEntity.getVehicle() instanceof RideableEntity rideable) {
                rideable.doFactory((factoryNotNull)->{
                    factoryNotNull.getProductFunction().runScript(new EventToJS("onPress", inputKey, pAction));
                });
            }
        }
    }
    @Inject(method = "onMove", at = @At("HEAD"))
    private void onMove(long window, double x, double y, CallbackInfo ci) {
        // 处理鼠标移动事件
        double deltaX = x - lastX;
        double deltaY = y - lastY;
        Player player = minecraft.player;
        if (player != null) {
            if (player.getVehicle() instanceof ComponentEntity componentEntity) {
                ComponentFL componentFL= componentEntity.getComponentFL();
                if (componentFL instanceof SeatFL seatBase) {
                    seatBase.addMouseDelta(deltaX, deltaY);
                }

            }
        }


        if (player != null && player.getVehicle() instanceof ComponentEntity componentEntity) {
            componentEntity.doFactory((factoryNotNull)->{
                factoryNotNull.getProductFunction().runScript(new EventToJS("onMove", deltaX, deltaY, x, y));
            });
            if (componentEntity.getVehicle() instanceof RideableEntity rideable) {
                rideable.doFactory((factoryNotNull)->{
                    factoryNotNull.getProductFunction().runScript(new EventToJS("onMove", deltaX, deltaY, x, y));
                });
            }
        }
        lastX = x;
        lastY = y;
    }
    @Inject(method = "onScroll", at = @At("HEAD"))
    private void onScroll(long pWindowPointer, double xOffset, double yOffset, CallbackInfo ci) {
        Player player = Minecraft.getInstance().player;
        if (player != null && player.getVehicle() instanceof ComponentEntity componentEntity) {
            componentEntity.doFactory((factoryNotNull)->{
                factoryNotNull.getProductFunction().runScript(new EventToJS("onScroll", xOffset, yOffset));
            });
            if (componentEntity.getVehicle() instanceof RideableEntity rideable) {
                rideable.doFactory((factoryNotNull)->{
                    factoryNotNull.getProductFunction().runScript(new EventToJS("onScroll", xOffset, yOffset));
                });
            }
        }
    }
}